"""
The TanhActivation class implements the hyperbolic tangent activation function.
"""
import numpy as np

class TanhActivation:

    def __init__(self):
        """
        No initialization necessary.
        """
        pass

    def forward_pass(self, batch_values):
        """
        Compute the tanh activation for the input values.
        Tanh function: f(x) = (e^x - e^(-x)) / (e^x + e^(-x))
        """
        self.inputs = batch_values
        # in this case we save the outputs because we can reuse them for
        # the backward pass
        self.outputs = np.tanh(batch_values)
        return self.outputs

    def backward_pass(self, dvalues):
        """
        Compute the gradient of the loss with respect to the input values.
        The derivative of the tanh function is f'(x) = 1 - f(x)^2
        """
        self.dvalues = dvalues * (1 - self.outputs ** 2)
        return self.dvalues